package com.ruoyi.common.config;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class GeoConfig {

    public static final int orgId = 100;
    private static String http = "http://192.168.100.11";
    /** 新增围栏 **/
    public static final String enclosureAdd = "/positionApi/api/enclosure/add";
    public static final String enclosureUpdate = "/positionApi/api/enclosure/update";
    public static final String enclosureDel = "/positionApi/api/enclosure/delete/";
//    public static final String enclosureList = "/positionApi/api/enclosure/list";
    public static final String enclosureList = "/positionApi/api/fence/list";


    /** 新增报警规则 **/
    public static final String warnTriggerAdd = "/positionApi/api/warnTrigger/add";
    public static final String warnTriggerUpdate = "/positionApi/api/warnTrigger/update";
    public static final String warnTriggerDel = "/positionApi/api/warnTrigger/delete";

    /** 地图数据 GET /positionApi/api/area/allList/{orgId}?access_token= **/
    public static final String areaDataUrl = "/positionApi/api/area/allList/";

    /** 人员数据 **/
    public static final String allStaffUrl = "/positionApi/api/staff/dataList";
    /** 人员位置 **/
    public static final String positionUrl = "/positionApi/api/record/location/tracking";
    /** 订阅 **/
    public static final String subscribeList = "/positionApi/api/subscribe/list";

    public static void main(String[] args) {

    }


    public static JSONObject sendGet(String url) {
        log.error("发送 GET 请求, 请求url {}", url);
        HttpResponse execute = HttpRequest
                .get(http + url)
                .charset("UTF-8")
                .header("authorization", PersonPosition.getToken())
                .timeout(5000)
                .execute();
        int status = execute.getStatus();
        if (status != 200) {
            log.error("请求失败 状态码：{}",status);
            return new JSONObject();
        }
        String body = execute.body();
        return JSONUtil.parseObj(body);
    }


    public static String sendPost(String url, Map<String, Object> requestMap) {
        log.error("发送 POST 请求, 请求url {}, 请求参数 {}", url, requestMap);
        HttpResponse execute = HttpRequest
                .post(http + url + "?access_token=" + PersonPosition.getToken())
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(requestMap))
                .contentType("application/json")
                .timeout(5000)
                .execute();
        int status = execute.getStatus();

        if (status != 200) {
            log.error("请求失败 状态码：{}",status);
            return null;
        }
        String body = execute.body();
        log.info("请求返回: {}",body);
        return body;
    }


//    @Autowired
//    private RedisCache redisCache;
//    /**
//     * 获取areaId - 地图ID
//     * @return
//     */
//    public Long getAreaId() {
//        /** 缓存中获取 areaID **/
//        redisCache.getCacheObject("");
//        if (redisCache.hasKey("geo_area_id")) {
//            JSONArray jsonArray = redisCache.getCacheObject("geo_area_id");
//            JSONObject jsonObj = jsonArray!=null && !jsonArray.isEmpty() ? jsonArray.getJSONObject(0) : null;
//            return jsonObj!=null && jsonObj.containsKey("id") ? jsonObj.getLong("id") : null;
//        }
//
//        /** 缓存中不存在，重新请求 **/
//        JSONObject jsonObject = sendGet(areaDataUrl + orgId + "?access_token=" + PersonPosition.getToken());
//        JSONArray jsonArray = jsonObject.containsKey("data") ? jsonObject.getJSONArray("data") : null;
//        redisCache.setCacheObject("geo-area-id", jsonArray, 24, TimeUnit.HOURS);
//        JSONObject jsonObj = jsonArray!=null && !jsonArray.isEmpty() ? jsonArray.getJSONObject(0) : null;
//        return jsonObj!=null && jsonObj.containsKey("id") ? jsonObj.getLong("id") : null;
//    }

//    data object [] 非必须
//├─ id number 必须 areaId
//├─ parentId null,number 必须
//├─ name string 必须 地图名称
//├─ length double 必须 长
//├─ width double 必须 宽
//├─ path string 必须 地图地址

//    public static Map<String, Object> sendRequestForJsonData(String url, Map<String, Object> requestMap, Method method) {
//        HttpRequest httpRequest = new HttpRequest(url)
//                .header(Header.CONTENT_TYPE, "application/json; charset=UTF-8")
//                .header(Header.ACCEPT, "application/json")
//                .header(Header.ACCEPT_ENCODING, "");
//
//        httpRequest.method(method);
//        httpRequest.body(new JSONObject(requestMap).toString());
//
//        // 执行请求
//        HttpResponse response = httpRequest.execute();
//        Map map = new HashMap<String, Object>();
//        try {
//            String body = response.body();
//            map = new ObjectMapper().readValue(body, Map.class);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return map;
//    }

//    /**
//     * 根据围栏ID获取详情
//     * @return
//     */
//    public static JSONObject enclosure(String id) {
//        String url = http + "/positionApi/api/warnTrigger/getEnclosureById/"+id+"?access_token=" + PersonPosition.getToken();
//
//        HttpResponse httpResponse = HttpRequest
//                .get(url)
//                .timeout(3000)
//                //.form("type", 1)
//                .execute();
//        log.info("围栏详情：{}", httpResponse.toString());
//        int status = httpResponse.getStatus();
//        if (status != 200) {
//            return null;
//        } else {
//            String body = httpResponse.body();
//            JSONObject jsonObject = JSONUtil.parseObj(body);
//            JSONObject data = jsonObject.getJSONObject("data");
//            return data;
//        }
//    }
//
//
//    public static Object count() {
//        String url = http + "/positionApi/api/statistics/getPositionCount?access_token=" + PersonPosition.getToken();
//
//        JSONObject param = new JSONObject();
//        param.set("orgId", "100");
//
//        HttpResponse httpResponse = HttpRequest
//                .post(url)
//                .header(Header.CONTENT_TYPE, "application/json")
//                .timeout(3000)
//                .body(JSONUtil.toJsonStr(param))
//                .execute();
//        log.info("获取登录实时总数统计 {}", httpResponse.toString());
//        int status = httpResponse.getStatus();
//        if (status != 200) {
//            return null;
//        } else {
//            String body = httpResponse.body();
//            JSONObject jsonObject = JSONUtil.parseObj(body);
//            return jsonObject;
//        }
//    }

}
